package com.eep.shipping;

import com.eep.common.Status;
import com.eep.data.DataService;
import com.eep.data.DataServiceImpl;
import com.eep.data.DatabaseName;
import com.eep.logging.LogEvent;
import com.eep.logging.LogType;
import com.eep.logging.LogEventType;
import com.eep.logging.LoggingService;
import com.eep.logging.LoggingServiceImpl;
import com.eep.order.Order;
import com.eep.order.OrderDetailed;
import com.eep.order.OrderLineItem;
import com.eep.order.OrderSummary;
import java.util.ArrayList;
import java.util.List;

public class ShippingServiceImpl implements ShippingService {

    private DataService dataService = null;
    public ShippingServiceImpl() {
        dataService = new DataServiceImpl();
        dataService.createConnection(DatabaseName.OrderInfo);
    }

    public List<OrderSummary> getOrders() {
        List<OrderSummary> resultList = new ArrayList<OrderSummary>();
        for (OrderSummary order : dataService.getOrdersSummary()) {
            resultList.add(order);
        }
        return resultList;
    }

    public List<OrderSummary> getPendingOrders() {
        List<OrderSummary> resultList = new ArrayList<OrderSummary>();
        for (OrderSummary o : getOrders()) {
            if (!o.isShipped()) {
                resultList.add(OrderSummary.class.cast(o));
            }
        }
        return resultList;
    }

    public List<OrderSummary> getShippedOrders() {
        List<OrderSummary> resultList = new ArrayList<OrderSummary>();
        for (OrderSummary o : getOrders()) {
            if (o.isShipped()) {
                resultList.add(OrderSummary.class.cast(o));
            }
        }
        return resultList;
    }

    public OrderDetailed getDetailedOrder(int orderId) {
        OrderDetailed result = null;
        Order o = dataService.readOrder(orderId);
        if (OrderDetailed.class.isInstance(o)) {
            result = OrderDetailed.class.cast(o);
        }
        return result;
    }

    public Status markOrderAsShipped(int orderId, boolean shipped) {
        Status status = new Status();
        dataService.updateOrder(orderId, shipped);
        status.setSuccess(true);
        
        LoggingService logging = new LoggingServiceImpl();
        logging.logAppEvent(new LogEvent("GenericUser", LogType.Shipping, LogEventType.OrderShipped, orderId));
        
        return status;
    }

    public static void main(String args[]) {
        ShippingService s = new ShippingServiceImpl();
        System.out.println("Pending Orders (Summary Only)");
        System.out.println("-------------------------------------------------");

        for (OrderSummary o : s.getPendingOrders()) {
            System.out.println(o);
        }
        
        System.out.println("Shipped Orders (Summary Only)");
        System.out.println("-------------------------------------------------");
        for (OrderSummary o : s.getShippedOrders()) {
            System.out.println(o);
        }

        System.out.println("Detailed Orders");
        System.out.println("-------------------------------------------------");
        for (OrderSummary o : s.getOrders()) {
            System.out.println("Requesting a detailed order for order id = '" + o.getId() + "'");
            OrderDetailed od = s.getDetailedOrder(o.getId());
            for (int i = 0; i < od.getOrderLineItemCount(); i++) {
                OrderLineItem oli = od.getOrderLineItem(i);
                System.out.println("  OrderLineItem - id: " + oli.getProductId());
                System.out.println("  OrderLineItem - desc: " + oli.getDescription());
                System.out.println("  OrderLineItem - price: " + oli.getItemPrice());
            }
        }
    }

}
